package thread;
/**
 * 互斥性:
 * 当使用多个synchronized锁定多个不同的代码片段，并且指定的同步监视器对象相同时，
 * 这些代码片段之间就是互斥的，多个线程不能同时操作它们
 */
public class SyncDemo5 {
    public static void main(String[] args) {
        Coo c = new Coo();
        Thread t1 = new Thread(c::Aoo);
        Thread t2 = new Thread(c::Boo);
        t1.start();
        t2.start();
    }
}
class Coo{
    public synchronized void Aoo(){
        Thread t = Thread.currentThread();
            try {
                System.out.println(t.getName()+"：Aoo方法开始");
                Thread.sleep(2000);
                System.out.println(t.getName()+"：Aoo方法结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    }
    public synchronized void Boo(){
        Thread t = Thread.currentThread();
            try {
                System.out.println(t.getName()+"：Boo方法开始");
                Thread.sleep(2000);
                System.out.println(t.getName()+"：Boo方法结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    }
}
